package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.LogisticsFeeRules;
import com.example.mall.dto.LogisticsFeeRulesDto;
import com.example.mall.mapper.LogisticsFeeRulesMapper;
import com.example.mall.query.LogisticsFeeRulesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ILogisticsFeeRulesService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物流费用规则表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class LogisticsFeeRulesServiceImpl extends ServiceImpl<LogisticsFeeRulesMapper, LogisticsFeeRules> implements ILogisticsFeeRulesService {

    @Autowired
    private LogisticsFeeRulesMapper logisticsFeeRulesMapper;

    @Override
    public PageDTO<LogisticsFeeRulesDto> queryPage(LogisticsFeeRulesQuery logisticsFeeRulesQuery) {
        if (logisticsFeeRulesQuery == null) {
            logisticsFeeRulesQuery = new LogisticsFeeRulesQuery();
        }
        Page<LogisticsFeeRules> page = logisticsFeeRulesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<LogisticsFeeRules> cartPage = lambdaQuery()
                .like(logisticsFeeRulesQuery.getRuleId()!= null, LogisticsFeeRules::getRuleId, logisticsFeeRulesQuery.getRuleId())
                .like(logisticsFeeRulesQuery.getCompanyId()!= null, LogisticsFeeRules::getCompanyId, logisticsFeeRulesQuery.getCompanyId())
                .like(logisticsFeeRulesQuery.getDestination()!= null, LogisticsFeeRules::getDestination, logisticsFeeRulesQuery.getDestination())
                .like(logisticsFeeRulesQuery.getWeightMin()!= null, LogisticsFeeRules::getWeightMin, logisticsFeeRulesQuery.getWeightMin())
                .like(logisticsFeeRulesQuery.getWeightMax()!= null, LogisticsFeeRules::getWeightMax, logisticsFeeRulesQuery.getWeightMax())
                .like(logisticsFeeRulesQuery.getVolumeMax()!= null, LogisticsFeeRules::getVolumeMax, logisticsFeeRulesQuery.getVolumeMax())
                .like(logisticsFeeRulesQuery.getVolumeMin()!= null, LogisticsFeeRules::getVolumeMin, logisticsFeeRulesQuery.getVolumeMin())
                .like(logisticsFeeRulesQuery.getFee()!= null, LogisticsFeeRules::getFee, logisticsFeeRulesQuery.getFee())
                .like(logisticsFeeRulesQuery.getIsActive()!= null, LogisticsFeeRules::getIsActive, logisticsFeeRulesQuery.getIsActive())
                .orderByDesc(LogisticsFeeRules::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, LogisticsFeeRulesDto.class);
    }

    @Override
    public LogisticsFeeRules queryLogisticsFeeRulesById(Long ruleId) {
        AssertUtil.isNotNull(ruleId, "物流费用规则ID不能为空");
        return logisticsFeeRulesMapper.selectById(ruleId);
    }

    @Override
    public List<LogisticsFeeRules> queryLogisticsFeeRulesList() {
        return this.list();
    }

    @Override
    public void insert(LogisticsFeeRulesDto logisticsFeeRulesDto) {
        LogisticsFeeRules logisticsFeeRules = new LogisticsFeeRules();
        CopyUtils.copyProperties(logisticsFeeRulesDto, logisticsFeeRules);
        logisticsFeeRules.setRuleId(CodeGenerateUtils.generateUniqueId());
        logisticsFeeRules.setCreateDate(new Date());
        logisticsFeeRules.setUpdateDate(new Date());
        logisticsFeeRules.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        logisticsFeeRulesMapper.insert(logisticsFeeRules);
    }

    @Override
    public void update(LogisticsFeeRulesDto logisticsFeeRulesDto) {
        AssertUtil.isNotNull(logisticsFeeRulesDto.getRuleId(), "物流费用规则ID不能为空");
        LogisticsFeeRules logisticsFeeRules = logisticsFeeRulesMapper.selectById(logisticsFeeRulesDto.getRuleId());
        AssertUtil.isNotNull(logisticsFeeRules, "物流费用规则不存在");
        CopyUtils.copyProperties(logisticsFeeRulesDto, logisticsFeeRules);
        logisticsFeeRules.setUpdateDate(new Date());
        logisticsFeeRulesMapper.updateById(logisticsFeeRules);
    }

    @Override
    public void delete(Long ruleId) {
        AssertUtil.isNotNull(ruleId, "物流费用规则ID不能为空");
        LogisticsFeeRules logisticsFeeRules = logisticsFeeRulesMapper.selectById(ruleId);
        AssertUtil.isNotNull(logisticsFeeRules, "物流费用规则不存在");
        logisticsFeeRules.setIsActive(BaseConstants.LOSE_STATUS);
        logisticsFeeRules.setUpdateDate(new Date());
        logisticsFeeRulesMapper.updateById(logisticsFeeRules);
    }

    @Override
    public void deleteBatch(List<Long> ruleIds) {
        AssertUtil.isNotNull(ruleIds, "物流费用规则ID列表不能为空");
        List<LogisticsFeeRules> logisticsFeeRules = logisticsFeeRulesMapper.selectBatchIds(ruleIds);
        AssertUtil.isNotNull(logisticsFeeRules, "物流费用规则不存在");
        for (LogisticsFeeRules logisticsFeeRule : logisticsFeeRules) {
            logisticsFeeRule.setIsActive(BaseConstants.LOSE_STATUS);
            logisticsFeeRule.setUpdateDate(new Date());
            logisticsFeeRulesMapper.updateById(logisticsFeeRule);
        }
    }
}
